package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Page;
import com.example.springboot.common.Result;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.utils.TokenUtils;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;

import com.example.springboot.service.ISolutionService;
import com.example.springboot.entity.Solution;

import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/solution")
    public class SolutionController {
    
    @Resource
    private ISolutionService solutionService;

    //用户编辑解决方案
    @PostMapping("/save")
    public Result save(@RequestBody Solution solution) {
        solution.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return solutionService.insert(solution);
    }

    //用户管理方案仓库
    @PostMapping("/manageSolution")
    public Result manageSolution(@RequestBody Solution solution) {
        solution.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return solutionService.manageSolution(solution);
    }

    //用户从回收站恢复方案
    @PostMapping("/recoverySolution")
    public Result recoverySolution(@RequestBody Solution solution) {
        solution.setUserId(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        return solutionService.recoverySolution(solution);
    }

    //管理员通过审核方案
    @PostMapping("/toPublish")
    public Result toPublish(@RequestBody Map<String, Integer> map) {
        return solutionService.toPublish(map.get("id"));
    }

    //管理员打回方案
    @PostMapping("/toUnPublish")
    public Result toUnPublish(@RequestBody Map<String, Integer> map) {
        return solutionService.toUnPublish(map.get("id"));
    }

    //管理员获取已发布方案列表
    @GetMapping("/adminGetPublished")
    public Result adminGetPublished(@RequestParam(defaultValue = "1") int PageNum,
                                    @RequestParam(defaultValue = "5") int PageSize,
                                    @RequestParam(defaultValue = "")  Integer userId,
                                    @RequestParam(defaultValue = "") String name,
                                    @RequestParam(defaultValue = "new") String sortWay) {
        return solutionService.adminGetPublished(PageNum,PageSize,userId,name,sortWay);
    }

    //管理员获取待审核方案列表
    @GetMapping("/adminGetToExamine")
    public Result adminGetToExamine(@RequestParam(defaultValue = "1") int PageNum,
                                    @RequestParam(defaultValue = "5") int PageSize,
                                    @RequestParam(defaultValue = "")  Integer userId,
                                    @RequestParam(defaultValue = "") String name) {
        return solutionService.adminGetToExamine(PageNum,PageSize,userId,name);
    }

    //方案仓库根据id删除方案及收藏、评论、打分
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean result = solutionService.removeSolutionInfo(id);
        if (result)
            return Result.success(true);
        else
            return Result.error(Constants.CODE_600,"解决方案不存在");
    }

    //根据id批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(solutionService.removeByIds(ids));
    }

    //根据引导id查找
    @GetMapping("/guide/{guide_id}")
    public Result findByGuide(@PathVariable Integer guide_id){
        return solutionService.findByGuide(guide_id);
    }

    //查找当前用户个人中心方案信息
    @GetMapping("/findByUserId")
    public Result findByUserId() {
        return Result.success(solutionService.getCenter(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId()));
    }

    //根据id查找方案详情(已发布)
    @GetMapping("/getDetails")
    public Result getDetails(@RequestParam Integer solution_id){
        return Result.success(solutionService.getDetailById(solution_id));
    }

    //根据id查找方案
    @GetMapping
    public Result getById(@RequestParam Integer id) {
        if (id == null)
            throw new ServiceException(Constants.CODE_500,"系统错误");
        Solution solution;
        try {
            solution = solutionService.getById(id);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (solution == null)
            throw new ServiceException(Constants.CODE_600,"未找到方案");
        return Result.success(solution);
    }

    //方案社区展示已发布方案
    @GetMapping("/findAllBySearch")
    public Result findAllBySearch(@RequestParam(defaultValue = "1") int PageNum,
                          @RequestParam(defaultValue = "10") int PageSize,
                          @RequestParam(defaultValue = "") String words){
        HashMap<String, Page> hashMap = solutionService.findAllBySearch(PageNum,PageSize,words);
        if (hashMap == null)
            return Result.error(Constants.CODE_600,"未搜到相关信息");
        return Result.success(hashMap);
    }

    //创意访问量+1
    @PostMapping("/updateView")
    public Result updateView(@RequestBody Map<String, Integer> map) {
        return Result.success(solutionService.updateView(map.get("id")));
    }

    //导出方案详情
    @CrossOrigin(origins = "http://182.92.155.101/")
    @GetMapping("/export")
    public void export(@RequestParam Integer guideId, HttpServletResponse response) {
        solutionService.export(guideId,response);
    }
}

